import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import about from '../views/AboutView.vue'
import module from '../views/module.vue'

import { useUserStore } from '../store.js';


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/login' // 默认重定向到登录页面
    },
    {
      path: '/login',
      component: () => import('../views/login.vue'),
    },
    {
      path: '/home',
      name: 'home',
      component: HomeView,
    meta: { requiresAuth: true },
    },
    {
      path: '/about',
      name: 'about',
      component: about,
      meta: { requiresAuth: true },
    },
    {
      path: '/module',
      name: 'module',
      component:module,
      meta: { requiresAuth: true },
    }

  ]
})

router.beforeEach((to, from, next) => {
  const userStore = useUserStore();
  if (to.meta.requiresAuth && !userStore.isLoggedIn) {
    next('/login');
  } else {
    next();
  }
});




export default router



